home *** CD-ROM | disk | FTP | other *** search
/ Cream of the Crop 1 / Cream of the Crop 1.iso / PROGRAM / SNIP1292.ARJ / TRUENAME.C < prev    next >
C/C++ Source or Header  |  1992-07-03  |  2KB  |  99 lines

  1. /*
  2. ** Apologies for the grotty code; I only just whipped this up.
  3. **
  4. ** tname.c -- wrapper for the undocumented DOS function TRUENAME
  5. **
  6. ** TRUENAME: interrupt 0x21 function 0x60
  7. **
  8. **   Call with: ah    =  60h
  9. **              es:di -> destination buffer
  10. **              ds:si -> source buffer
  11. **
  12. **   Returns:   carry bit set if there were problems
  13. **
  14. ** This code hereby contributed to the public domain.
  15. */
  16.  
  17. #include <string.h>
  18. #include <dos.h>
  19.  
  20. /*
  21. ** Strip leading and trailing blanks from a string.
  22. */
  23.  
  24. char _far *strip(char _far *s)
  25. {
  26.       char _far *end;
  27.  
  28.       for ( ; isspace(*s); s++)
  29.             ;
  30.  
  31.       for (end = s; *end; end++)
  32.             ;
  33.  
  34.       for (end--; isspace(*end); *end-- = '\0')
  35.             ;
  36.  
  37.       return s;
  38. }
  39.  
  40. /*
  41. ** Truename itself. Note that I'm using intdosx() rather than
  42. ** playing with some inline assembler -- I've discovered some
  43. ** people that actually don't have an assembler, poor bastards :-)
  44. */
  45.  
  46. char _far *truename(char _far *dst, char _far *src)
  47. {
  48.       union REGS rg;
  49.       struct SREGS rs;
  50.  
  51.       if (!src || !*src || !dst)
  52.             return NULL;
  53.  
  54.       src=strip(src);
  55.  
  56.       rg.h.ah=0x60;
  57.       rg.x.si=FP_OFF(src);
  58.       rg.x.di=FP_OFF(dst);
  59.       rs.ds=FP_SEG(src);
  60.       rs.es=FP_SEG(dst);
  61.  
  62.       intdosx(&rg,&rg,&rs);
  63.  
  64.       return (rg.x.flags&1)?NULL:dst;
  65. }
  66.  
  67. #ifdef TEST
  68.  
  69. #include <stdio.h>
  70.  
  71. /*
  72. ** ... and a little test function.
  73. */
  74.  
  75. int main(int argc, char *argv[])
  76. {
  77.       char buf[128]="                             ", _far *s;
  78.       int i;
  79.  
  80.       if (3 > _osmajor)
  81.       {
  82.             puts("Only works with DOS 3+");
  83.             return -1;
  84.       }
  85.       if(argc > 1)
  86.       {
  87.             for(i = 1; i < argc; i++)
  88.             {
  89.                   s = truename((char _far *)buf,(char _far *)argv[i]);
  90.                   printf("%s=%s\n",argv[i], s ? buf : "(null)");
  91.             }
  92.       }
  93.       else  printf("Usage: TRUENAME [filename [filename...]]\n");
  94.  
  95.       return 0;
  96. }
  97.  
  98. #endif
  99.